/*******************************************************************************

This code file prepares a BIN to BBL crosswalk using the ADR file of the PAD.

*******************************************************************************/

*** Manage settings

	run "~/Dropbox (MIT)/Research/NYC421a/code/modules/settings.do"

*** Load ADR table of PAD

	import delimited "$data/raw/PAD/bobaadr.txt", encoding(ISO-8859-1)
	
*** Create xwalk

	keep boro block lot bin
	
	gduplicates drop
	
	tostring boro block lot, replace
	
	replace block = "0"+block if length(block)<5
	replace block = "0"+block if length(block)<5
	replace block = "0"+block if length(block)<5
	replace block = "0"+block if length(block)<5
	
	replace lot = "0"+lot if length(lot)<4
	replace lot = "0"+lot if length(lot)<4
	replace lot = "0"+lot if length(lot)<4
	
	gen bbl = boro+block+lot
	
	drop block lot boro
	
	destring bbl, replace
	
	format bbl %18.0f
	
	* Drop uninformative BINs
	duplicates drop bin, force
	
* Fix missing BINs

	merge 1:1 bin using "$data/raw/error_files/match_fails.dta", nogen
	replace bbl = bbl_new if !missing(bbl_new)
	drop bbl_new
	
* Save
	
	save "$data/clean/bin_to_bbl.dta", replace
